package com.hz.foodstalls.controller;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.hz.foodstalls.interceptor.AdminInterceptor;
import com.hz.foodstalls.model.Resource;
import com.hz.foodstalls.model.Role;
import com.hz.foodstalls.model.RoleResource;
import com.jfinal.aop.Before;
import com.jfinal.ext.plugin.shiro.JdbcShiroPlugin;
import com.jfinal.ext.plugin.shiro.ShiroKit;
import com.jfinal.ext.plugin.shiro.ShiroPlugin;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.plugin.activerecord.Page;

/**
 * ResourceController, Generated by Robot on 2016-9-21 11:15:23.
 */
@Before(AdminInterceptor.class)
@ControllerBind(controllerKey = "/resource", viewPath = "/WEB-INF/views/resource/")
public class ResourceController extends BaseController {

    private final String MODULE_NAME = "/resource";

    public void index() {
        Page<Resource> page = Resource.dao.page(getParaToInt("pageNumber", 1), getParaToInt("pageSize", 10));
        List<Resource> rlist = page.getList();
        Map<Integer, String> map = new HashMap<>();
        for (Resource resource : rlist) {
            if (resource.getPid() == 0) {
                map.put(0, "/");
                continue;
            }

            map.put(resource.getPid(), Resource.dao.getPNameByid(resource.getPid()));
        }
        setAttr("ResourceMap", map);
        setAttr("page", page);
        render("index.html");
    }

    public void save() {
        Resource model = getModel(Resource.class, "");
        boolean isSave = model.getId() == null;

        if (Resource.dao.saveOrUpdate(model, isSave)) {
            JdbcShiroPlugin.reSetShiro(this);
            redirect(MODULE_NAME);
        } else
            renderError(501);
    }

    public void edit() {
        Resource resource = Resource.dao.findById(getParaToInt("id", 0));
        List<Resource> resources = Resource.dao.findAllResource();
        Map<String, String> map = new LinkedHashMap<>();
        map.put("0", "/");
        for (Resource res : resources) {

            map.put(String.valueOf(res.getId()), Resource.dao.getPNameByid(res.getId()));
        }

        setAttr("ResourceMap", map);

        if (resource == null)
            resource = new Resource();

        setAttr("resource", resource);
        render("edit.html");
    }

    public void delete() {
        String[] resourceSetIds = getParaValues("ids");
        StringBuilder success = new StringBuilder();
        StringBuilder error = new StringBuilder();
        for (String resourceSetId : resourceSetIds) {
            Resource resource = Resource.dao.findById(resourceSetId);
            List<RoleResource> roleresources = RoleResource.dao.find("select*from role_resource where resourceSet_id=?",
                    resourceSetId);
            if (roleresources == null || roleresources.isEmpty()) {
                boolean resourcedelete = Resource.dao.deleteById(resourceSetId);
                Resource.dao.deleteResourceByPid(resourceSetId);
                if (resourcedelete)

                    success.append("权限名[" + resource.getName() + "及其子权限]删除成功   ");
                else
                    error.append("权限名[" + resource.getName() + "]删除失败   ");
            } else {
                for (RoleResource roleResource : roleresources) {
                    Role role = Role.dao.findById(roleResource.getRolesetId());
                    error.append("删除失败,权限名:[" + resource.getName() + "]已与角色名[" + role.getName() + "]绑定 ");
                }

            }

        }
        if (error == null || error.length() == 0) {
            ajaxJsonSuccessMessage(success.toString());
        } else {
            ajaxJsonErrorMessage(error.toString());
        }
    }
}
